package com.library.system.controller;

import cn.hutool.core.util.StrUtil;
import com.library.common.annotation.CurrentUser;
import com.library.common.enums.AuditStatusEnum;
import com.library.common.enums.ClassifyEnum;
import com.library.common.service.bo.CurrentLoginUser;
import com.library.logging.annotation.LogSys;
import com.library.system.modules.examine.vo.ExamineVO;
import com.library.system.modules.examine.bo.ExaminePage;
import com.library.system.modules.examine.bo.ExamineInsert;
import com.library.system.modules.examine.bo.ExamineUpdate;
import com.library.system.modules.examine.service.ExamineService;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.library.common.response.Result;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;

import java.time.LocalDateTime;
import java.util.Objects;


/**
 * @Author xyh
 * @Desc 审核表(Examine)表控制层
 * @Date 2023-10-05 16:35:22
 */

@RestController
@RequestMapping("examine")
public class ExamineController {

    @Resource
    private ExamineService examineService;


    /**
     * 公告审核分页查询列表
     *
     * @return 数据
     */
    @LogSys(value = "公告审核分页查询列表")
    @GetMapping("/list")
    public Result<IPage<ExamineVO>> noticeQueryByPage(@Valid ExaminePage page) {
        return Result.success(examineService.queryByPage(page));
    }

    /**
     * 图书归还审核分页查询列表
     *
     * @return 数据
     */
    @LogSys(value = "图书归还审核分页查询列表")
    @GetMapping("/bookaudit/list")
    public Result<IPage<ExamineVO>> returnBookByPage(@Valid ExaminePage page) {
        page.setClassify(ClassifyEnum.RETURN_BOOK.getCode());
        return Result.success(examineService.queryByPage(page));
    }

    /**
     * 根据ID获取数据
     *
     * @return 单个审核表数据
     */
    @LogSys(value = "单个审核表数据")
    @PostMapping("/queryById/{id}")
    public Result<ExamineVO> queryById(@PathVariable("id") Integer id) {
        return Result.success(examineService.queryById(id));
    }


    /**
     * 审核通过
     *
     * @param param
     * @param currentLoginUser
     * @return
     */
    @LogSys(value = "审核通过")
    @PutMapping("/success")
    public Result examineSuccess(@Valid @RequestBody ExamineUpdate param, @CurrentUser CurrentLoginUser currentLoginUser) {
        if (Objects.isNull(param.getId())) {
            return Result.error("ID不能为空");
        }
        param.setUsername(currentLoginUser.getUsername());
        param.setExamineStatus(AuditStatusEnum.AUDIT_SUCCESS.getCode());
        param.setFinishTime(LocalDateTime.now());
        examineService.examineUpdate(param);
        return Result.success();
    }

    /**
     * 审核失败
     *
     * @param param
     * @param currentLoginUser
     * @return
     */
    @LogSys(value = "审核失败")
    @PutMapping("/fail")
    public Result examineFail(@Valid @RequestBody ExamineUpdate param, @CurrentUser CurrentLoginUser currentLoginUser) {
        if (Objects.isNull(param.getId())) {
            return Result.error("ID不能为空");
        }
        if (StrUtil.isEmpty(param.getAdvice())) {
            return Result.error("审核失败，失败原因不能为空！");
        }
        param.setUsername(currentLoginUser.getUsername());
        param.setExamineStatus(AuditStatusEnum.REJECT.getCode());
        param.setFinishTime(LocalDateTime.now());
        examineService.examineUpdate(param);
        return Result.success();
    }

    /**
     * 删除审核表
     *
     * @return 删除审核表数据
     */
    @LogSys(value = "删除审核表数据")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        examineService.deleteById(id);
        return Result.success();
    }
}
